/**
 * class  RadioGroup
 * 单选框组，可获得组的所有Value，和选中Value,也可选中组中所有多选框
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.controls
{
	import flash.events.Event;
	
	import as3.component.Component;
	import as3.component.support.BaseGroup;
	import as3.component.support.BaseRadio;

	[Event(name="change", type="flash.events.Event")]
	
	public class RadioGroup extends BaseGroup
	{
		
		private var _selectItem:BaseRadio;
		
		public function RadioGroup()
		{
			super();
		}
		
		override public function put(item:Component):void
		{
			if (item is BaseRadio)
			{
				if (_list == null) _list = new Array();
				if (_list.indexOf(item) == -1) _list.push(item);
			}
		}
		
		public function selelct(selectValue:BaseRadio):void
		{
			if (_list == null || selectValue == null || _list.indexOf(selectValue) == -1) return;
			if (_selectItem == selectValue) return;
			_selectItem = selectValue;
			var item:BaseRadio;
			for (var i:int = 0 ; i < _list.length; i++)
			{
				item = _list[i] as BaseRadio;
				if (item != _selectItem) item.selected = false;
			}
			selectValue.selected = true;
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function selelctByValue(value:*):void
		{
			if (!value || value == getSelectValue()) return;
			var item:BaseRadio;
			for (var i:int = 0 ; i < _list.length; i++)
			{
				item = _list[i] as BaseRadio;
				if (item.value == value) item.selected = true;
				else item.selected = false;
			}
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		public function getAllValue():Array
		{			
			if (_list == null) return null;
			
			var return_ary:Array = new Array();
			var item:BaseRadio;
			for (var i:int = 0 ; i < _list.length; i++)
			{
				item = _list[i] as BaseRadio;
				return_ary.push(item.value);
			}
			return return_ary;
		}
		
		public function getSelectValue():*
		{			
			if (_selectItem == null) return null;
			return _selectItem.value;
		}
		
		public function getSelelctRadio():BaseRadio
		{
			return _selectItem;
		}
		
		override public function unSelectAll():void
		{
			if (_selectItem) _selectItem.selected = false;
		}
	}
}